package com.shanjupay.merchant.controller;

import com.shanjupay.common.util.SecurityUtil;
import com.shanjupay.merchant.api.MerchantService;
import com.shanjupay.merchant.api.dto.MerchantDTO;
import com.shanjupay.merchant.convert.MerChantDedailConvert;
import com.shanjupay.merchant.convert.MerchantRegisterConvert;
import com.shanjupay.merchant.service.FileService;
import com.shanjupay.merchant.service.SmsService;
import com.shanjupay.merchant.vo.MerchantDetailVO;
import com.shanjupay.merchant.vo.MerchantRegisterVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *     商户应用层
 * </p>
 *
 * @author <a href=mailto:zhaoboying@itcast.cn> byZhao </a>
 * @version 1.0.0
 * @motto Talk is cheap, show me the code!
 * @since 2022-04-29 02:25:04
 */
@Slf4j
@RestController
public class MerchantController {

    @Reference
    private MerchantService merchantService;
    @Autowired
    private SmsService smsService;

    //查找商户
    @GetMapping(value = "/merchants/{id}")
    public MerchantDTO queryMerchantById(@PathVariable Long id) {
        MerchantDTO merchantDTO = merchantService.queryMerchantById(id);
        return merchantDTO;
    }

    //注册时发送验证码
    @GetMapping("sms")
    @ApiOperation("获取手机验证码")
    @ApiImplicitParam(name = "phone",value = "手机号",required = true,dataType = "string",paramType = "query")
    public String getSmsCode(String phone){
        log.info("向手机号{}，发送验证码",phone);
        return smsService.sendMsg(phone);
    }

    //实现商户注册方法
    @PostMapping("/merchants/register")
    @ApiImplicitParam(name = "merchantRegisterVO", value = "注册信息", required = true, dataType = "MerchantRegisterVO", paramType = "body")
    @ApiOperation("商户注册")
    public MerchantRegisterVO registerMerchant(@RequestBody MerchantRegisterVO merchantRegisterVO) {
        // 校验验证码
        smsService.checkVerifyCode(merchantRegisterVO.getVerifyKey(), merchantRegisterVO.getVerifyCode());
// 优化前代码
//        MerchantDTO merchantDTO = new MerchantDTO();
//        BeanUtils.copyProperties(merchantRegisterVO, merchantDTO);
        // 优化后代码
        MerchantDTO merchantDTO = MerchantRegisterConvert
                .INSTANCE.vo2dto(merchantRegisterVO);
        // 注册商户 dubbo远程调用
        merchantService.createMerchant(merchantDTO);
        return merchantRegisterVO;
    }
    @Autowired
    FileService fileService;
    @PostMapping("/upload")
    @ApiOperation(value = "证件上传接口")
    @ApiImplicitParam(name = "file", value = "上传的文件", paramType = "form", dataType = "MultipartFile", required = true)
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {

        // 获取原始文件名称 ：例如XXX.jpg
        String originalFilename = file.getOriginalFilename();
        // 获取文件后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 生成文件名称 UUID.jpg
        String fileName = UUID.randomUUID().toString()+suffix;
        // 获取文件字节数组
        byte[] bytes = file.getBytes();
        String accessUrl = fileService.upload(bytes, fileName);
        return accessUrl;
    }

    /**
     * 商户资质申请
     * @param merchantDetailVO
     */
    @PostMapping("/my/merchants/save")
    @ApiOperation("商户资质申请")
    @ApiImplicitParam(name = "merchantDetailVO" ,value = "商户认证资料",required = true,dataType = "MerchantDetailVO",paramType = "body")
    public void saveMerchant(@RequestBody MerchantDetailVO merchantDetailVO){
        // 获取商户id
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO merchantDTO = MerChantDedailConvert.INSTANCE.vo2dto(merchantDetailVO);
        merchantService.applyMerchant(merchantDTO);

    }

}
